﻿using DevTeam;

using System.Data;

namespace ConfExpert.Forms.ImportWizards.Barkod
{
    public partial class GP : XImportWizard
    {
        public GP()
        {
            InitializeComponent();

            Init("GP Barkod Listesi", false);
            this.FileDone += GP_Barkod_FileDone;
            //btnProcessFolder.Enabled = true;
        }

        public void GP_Barkod_FileDone(DataSet ds, string FileName)
        {
            DataTable dtResult = ds.Tables[0];
            //dtResult = TransposeData(dtResult);

            MasterGrid.Init(dtResult);
            DetailGrid.Init(dtResult);  //TODO: burada hata mı var?

            MasterGrid.AddSummary();
            //MasterGrid.AddSummary("QTY");
            MasterGrid.MakeReadOnly();


        }

        private DataTable TransposeData(DataTable table)
        {
            DataTable dtNew = new DataTable();

            dtNew.Columns.Add("BOX", typeof(string));
            dtNew.Columns.Add("BOX NO", typeof(string));
            dtNew.Columns.Add("MODEL", typeof(string));
            dtNew.Columns.Add("ARTICLE", typeof(string));
            dtNew.Columns.Add("TAGLIA", typeof(string));
            dtNew.Columns.Add("QTY", typeof(int));

            int lastMeaningfulColumnIndex = 0;
            for (int i = 0; i < table.Rows[0].ItemArray.Length; i++)
            {
                var cellValue = table.Rows[0].ItemArray[i];
                if (cellValue.ToString().ToLower().Contains("tot"))
                {
                    lastMeaningfulColumnIndex = i;
                }
            }

            for (int rowIndex = 1; rowIndex < table.Rows.Count; rowIndex++)
            {
                DataRow dr = table.Rows[rowIndex];
                if (dr[1].ToInt(-1) > 0)
                {
                    for (int colIndex = 5; colIndex < lastMeaningfulColumnIndex; colIndex++)    //3:20 -> F4:F21... Son sütunun TOT olduğu varsayıldı!
                    {
                        object cellValue = dr[colIndex];
                        if (cellValue.ToInt(-1) > 0)
                        {
                            DataRow newRow = dtNew.NewRow();
                            newRow[0] = dr[0].ToString();
                            newRow[1] = dr[1].ToString();
                            newRow[2] = dr[2].ToString();// + dr[2].ToString().Contains("_A") ? "_AD" : string.Empty);
                            newRow[3] = dr[3].ToString();
                            newRow[4] = table.Rows[0][colIndex].ToString();
                            newRow[5] = dr[colIndex];
                            dtNew.Rows.Add(newRow);
                        }
                    }
                }
            }

            return dtNew;
        }
    }
}